<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script>

        // race 比赛
        //   谁先取得结果，就把谁的结果作为新的res的结果 , 不管成功或者失败

        const p1 = new Promise((resolve , reject) => {
            const xhr = new XMLHttpRequest() ;
            // 0
            xhr.open('get' , '../php/1.php' , true) ;
            // 1
            xhr.send() ;
            xhr.onreadystatechange = function() {
                // 2 3 4  函数会执行3次
                if(xhr.readyState === 4 && xhr.status === 200) {
                    let data = xhr.responseText ;
                    resolve(data) ;
                } 
            }
        })

        const p2 = new Promise((resolve , reject) => {
            const xhr = new XMLHttpRequest() ;
            // 0
            xhr.open('get' , '../php/2.php' , true) ;
            // 1
            xhr.send() ;
            xhr.onreadystatechange = function() {
                // 2 3 4  函数会执行3次
                if(xhr.readyState === 4 && xhr.status === 200) {
                    let data = xhr.responseText ;
                    resolve(data) ;
                } 
            }
        })


        const p3 = new Promise((resolve , reject) => {
            const xhr = new XMLHttpRequest() ;
            // 0
            xhr.open('get' , '../php/3.php' , true) ;
            // 1
            xhr.send() ;
            xhr.onreadystatechange = function() {
                // 2 3 4  函数会执行3次
                if(xhr.readyState === 4 && xhr.status === 200) {
                    let data = xhr.responseText ;
                    resolve(data) ;
                } 
            }
        })

        // race 比赛
        //   谁先取得结果，就把谁的结果作为新的res的结果
        const res = Promise.race([p1 , p2 , p3]) ;

        res.then(data => {
            console.log(data);
        })

    </script>
    
</body>
</html>